﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Typing_GUI.CLASS
{
   public class CMediaPlayer
   {
       // Dieu chinh am thanh
       [DllImport("winmm.dll")]
       public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

       [DllImport("winmm.dll")]
       public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
       // Thêm thư viện nhạc vào 
  [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]

    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
    public string Status
    {
        get
        {

            int length = 128;

            StringBuilder strBuffer = new StringBuilder(length);

            mciSendString("status MyMedia mode", strBuffer,

            length, IntPtr.Zero);

            return strBuffer.ToString();

        }

    }
       //Lấy chiều dài bài hát
    public long Length

    {

        get

        {

        StringBuilder fileleng = new StringBuilder(256);

        mciSendString("status MyMedia length", fileleng, 256, IntPtr.Zero);

        try

        {

        return (long)Math.Floor(Convert.ToDouble(fileleng.ToString().Trim()) / 1000);

        }

        catch { return 0; }

        }

    }
       //Lấy chiều dài hiện tại
    public long Current
  {

      get
      {

          StringBuilder fileleng = new StringBuilder(256);

          mciSendString("status MyMedia position", fileleng, 256, IntPtr.Zero);

          try
          {

              return (long)Math.Floor(Convert.ToDouble(fileleng.ToString().Trim()) / 1000);

          }

          catch { return 0; }

      }

  }
       //Hàm open
    public void Open(string filePath,System.Windows.Forms.Control c)
        {
            string sCommand = "open \"" + filePath + "\" type mpegvideo alias MyMedia style child parent " + c.Handle;
            mciSendString(sCommand, null, 0, IntPtr.Zero);
         }
      // Hàm Play
    public void Play()
    {
              mciSendString("play MyMedia", null, 0, IntPtr.Zero);
    }

    public void Pause()
    {

    mciSendString("pause MyMedia", null, 0, IntPtr.Zero);
    }

    public void Stop()

    {

    try{

    mciSendString("stop MyMedia", null, 0, IntPtr.Zero); }catch{}

    }

    public void Seek(long value)

    {

    value = value * 1000;

    mciSendString("seek MyMedia to " + value.ToString(), null, 0, IntPtr.Zero);

    Play();

    }

    public void Close()

    {

    try{

    mciSendString("close MyMedia", null, 0, IntPtr.Zero);

    }catch {}

    }
    //public void DisplayMediaWindow()
    //   {

    //       mciSendString("put MyMedia", null, 0, IntPtr.Zero);

    //   }
       public void DisplayMediaWindow(System.Windows.Forms.Control c)
       {
           mciSendString("put MyMedia window at 0 0 "+
    " " + c.Width.ToString()+ " " + c.Height.ToString(), null, 0, IntPtr.Zero);

       }
    //public void DisplayMediaWindow(System.Drawing.Rectangle  rec)

    //{

    //mciSendString("put MyMedia window at " + rec.Left + " " + rec.Top +

    //" " + rec.Width + " " + rec.Height, null, 0, IntPtr.Zero);

    //}
       public static string TimeFormat(long sec)
       {
           string kq = "";
          long seconds;
           long  minutes;
           minutes = sec / 60;
           seconds = sec % 60;
           string s;
           s = seconds.ToString();
           if (seconds < 10)
               s = s.PadLeft(2, '0');
           string m;
           m = minutes.ToString();
           if (minutes < 10)
               m = m.PadLeft(2, '0');
           kq = m + " : " + s;
           return kq;
       }
       public void PlayFullScreen()
       {
           // play an AVI movie full screen with callback notifications
           string CommandString = "Play MyMedia fullscreen notify";
       mciSendString(CommandString, null, 0, IntPtr.Zero);

       }

    }
 
}
